<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChContactBwDeleteRecordMapper;
use Hyperf\DbConnection\Db;
use Mine\Abstracts\AbstractService;
use Mine\MineModel;
use function Hyperf\Collection\data_get;

class ChContactBwDeleteRecordService extends AbstractService
{
    public $mapper;

    public function __construct(ChContactBwDeleteRecordMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    public function list(array $data): array
    {
        $pageSize = data_get($data,'page_size',MineModel::PAGE_SIZE);
        $keyword = data_get($data,'keyword');

        $paginate = Db::table('ch_contact_bw_delete_record as a')
            ->selectRaw('a.id,b.nickname,b.avatar,b.is_wechat,b.corp_short_name,a.type,a.join_time,a.delete_time')
            ->join('ch_contact as b','a.user_id','=','b.user_id')
            ->where('a.main_uid',$data['main_uid'])
            ->when(!empty($keyword),function ($query) use ($keyword){
                $query->where('b.nickname','like','%'.$keyword.'%');
            })->orderBy('a.delete_time','desc')
            ->paginate((int)$pageSize);

        return $this->mapper->setPaginate($paginate);
    }
}